﻿/*-------------------------------------*/
/* Ajax Core                           */
/* Created by ThanhTuanLe              */
/* Email: tuanitpro@gmail.com          */
/* Mobile: 097 6060 432                */
/* Website: tuanitpro.com              */
/*-------------------------------------*/

var xmlHttp = null;
// Dung doi tuong XMLHttpRequest load 
function loadXmlHttp() {

    if (window.XMLHttpRequest) { // IE7, Mozilla, Safari, Opera
        xmlHttp = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        try {
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); //IE 5.x, 6
        }
        catch (e) { }
    }
}
// Gui yeu cau thuc thi doi voi dia chi url
function sendRequest(url) {
    loadXmlHttp();

    if (xmlHttp) {
        loading();
        xmlHttp.open("GET", url, true); // true = async

        xmlHttp.onreadystatechange = onCallback;

        xmlHttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
        xmlHttp.send(null);
    }
}
function onCallback() {

    if (xmlHttp.readyState == 4) {

        if (xmlHttp.status == 200) {
            var r = document.getElementById('ajax-demo');
            r.innerHTML = xmlHttp.responseText;
        }
        else {
            alert('Error: ' + xmlHttp.status);
        }
    }
}


function loading() {
    document.getElementById("ajax-demo").innerHTML = '<br /><div id="loading"><img src="./images/loading.gif" alt="" title="Loading"/><br/>Loading...</div>';
}
function getContent(url) {
    //loadXmlHttp();
    //loading();
    sendRequest(url);
}
function callServerTime(){
    var rq = Math.random() * 100;
    var requestUrl = "AjaxServer.aspx?action=gettime&rq=" + rq;
    sendRequest(requestUrl);
}
function getProductDetail(pID) {
    var rq = Math.random() * 100;
    var requestUrl = "ajax.aspx?menu=detail&proid=" + encodeURIComponent(pID) + "&rq=" + rq;  
    sendRequest(requestUrl);
}
function getNewsCategory(cateID) {
    var rq = Math.random() * 100;
    var requestUrl = "ajax.aspx?menu=category&id=" + encodeURIComponent(cateID) + "&rq=" + rq;
    sendRequest(requestUrl);
}
function getProductCategoryPaging(cateID, page) {
    var rq = Math.random() * 100;
    var requestUrl = "ajax.aspx?menu=category&cateid=" + encodeURIComponent(cateID) + "&page=" + page + "&rq=" + rq;
    sendRequest(requestUrl);
}
function getProductPaging(p, page) {
    var rq = Math.random() * 100;
    var requestUrl = "ajax.aspx?page=" + page + "&rq=" + rq;
    sendRequest(requestUrl);
}
function getProductDetailPaging(pID, page) {
    var rq = Math.random() * 100;
    var requestUrl = "ajax.aspx?menu=detail&proid=" + encodeURIComponent(pID) + "&page=" + page + "&rq=" + rq;
    sendRequest(requestUrl);
}